<template>
  <el-dialog  draggable overflow :title="titleMap[mode]" v-model="visible" :width="700" destroy-on-close @closed="$emit('closed')">
    <sk-tabs type="border-card">
      <el-tab-pane ref="tab" label="信息">
        <el-form :model="form" :rules="rules" ref="dialogForm" label-width="60px" label-position="right">
          <el-row>
            <el-col :span="12">
              <el-card class="box-card">
                <el-col :span="22">
                  <el-form-item label="编号" prop="code">
                    <el-input v-model="form.code" clearable></el-input>

                  </el-form-item>
                </el-col>
                <el-col :span="22">
                  <el-form-item label="名称" prop="name">
                    <el-input v-model="form.name" clearable></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="22">
                  <el-form-item label="方向" prop="prefix_unit">
                    <skDicSelect v-model="form.direct_id" :code="'FinanceDirect'"></skDicSelect>
                  </el-form-item>
                </el-col>
                <el-col :span="22">
                  <el-form-item label="是否启用" prop="status">
                    <el-switch v-model="form.status_id" :active-value="0" :inactive-value="-1"></el-switch>
                  </el-form-item>
                </el-col>
                <el-col :span="22">
                  <el-form-item label="备注" prop="remark">
                    <el-input v-model="form.remark" clearable></el-input>
                  </el-form-item>
                </el-col>
              </el-card>
            </el-col>
            <el-col :span="12">

              <el-card class="box-card">
                <template #header>
                  <div class="card-header">
                    <span>核算项目</span>
                  </div>
                </template>
                <el-row>
                  <el-col :span="24">
                    <el-form-item label="数量项目" prop="needquantity">
                      <skDicCheckBox v-model="form.accounting" :code="'AccountingProject'"></skDicCheckBox>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="数量核算" prop="needquantity">
                      <el-switch v-model="form.needquantity" active-value="1" inactive-value="0"></el-switch>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="核算单位" prop="unit">
                      <el-input v-model="form.unit" clearable :disabled="form.needquantity == 0"></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="24">
                    <el-form-item label="交易单位" prop="trade_id">
                      <skDicSelect v-model="form.trade_id" :code="'trade'" :editsource="'default'">
                      </skDicSelect>
                    </el-form-item>
                  </el-col>
                </el-row>
              </el-card>
            </el-col>

          </el-row>
        </el-form>
      </el-tab-pane>
    </sk-tabs>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
    </template>
  </el-dialog>
</template>

<script>

import skDicSelect from "@/components/skDicSelect";
import skDicCheckBox from "@/components/skDicCheckBox";
export default {
  emits: ["success", "closed", "update:modeValue"],
  components: { skDicSelect, skDicCheckBox },
  data() {
    return {

      mode: "add",

      unit: "All",
      titleMap: {
        add: "新增科目",
        edit: "编辑科目",
      },
      form: {
        id: "",
        code: "",
        name: "",
        parent: { name: "" },
        remark: "",
        status_id: 0,
        accounting: ""
      },
      rules: {
        name: [{ required: true, message: "请输入名称" }],
        code: [{ required: true, message: "请输入编号" }],
      },

      visible: false,
      isSaveing: false,
    };
  },
  mounted() {


  },
  methods: {

    //显示
    close() {
      this.visible = false;
      this.$emit("update:modeValue", false);
      this.$emit("closed");
    },
    open(mode = "add") {
      this.mode = mode;
      this.visible = true;
      return this;
    },

    //表单提交方法
    submit() {
      this.$refs.dialogForm.validate(async (valid) => {
        if (valid) {
          this.isSaveing = true;

          this.form.accounting = (typeof (this.form.accounting ?? "") == "string") ? this.form.accounting : this.form.accounting.join(",");

          this.$API.basedata.subject.save.post(this.form).then(res => {
            if (res.code == 200) {
              this.$emit("success", this.form, this.mode);
              this.visible = false;
              this.$message.success("操作成功");
            }
          }).catch(() => {
            this.isSaveing = false;
          });
        }
      });
    },
    //表单注入数据
    setData(data) {
      if (data.parent == null) {
        data.parent = { name: "" };
      }
      Object.assign(this.form, data);
    },
  },
};
</script>

<style></style>
